package cn.chenc.framework.demo.controller;

import cn.chenc.framework.core.annotation.PreAuthorize;
import cn.chenc.framework.core.component.SpringContextHolder;
import cn.chenc.framework.core.model.AbstractDictData;
import cn.chenc.framework.core.model.response.AjaxResult;
import cn.chenc.framework.core.model.response.ResponseVO;
import cn.chenc.framework.web.model.response.ResultUtil;
import cn.chenc.framework.core.util.DictUtils;
import cn.chenc.framework.core.util.JsonUtils;
import cn.chenc.framework.core.util.file.FileUtils;
import cn.chenc.framework.demo.entity.Dict;
import cn.chenc.framework.demo.entity.User;
import cn.chenc.framework.demo.entity.UserCopy;
import cn.chenc.framework.demo.service.UserService;

//import cn.chenc.framework.mail.core.MailTemplate;
import cn.chenc.framework.poi.util.ExcelUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 　@description: TODO
 * 　@author secret
 * 　@date 2020/11/30 10:13
 *
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    RedisTemplate<String,Object> redisTemplate;
//    @Autowired
//    private MailTemplate mailTemplate;


    @GetMapping("/test")
    public ResponseVO test(){
        return ResultUtil.success("访问成功");
    }

    @GetMapping("/test2")
    public String test2(){
        return "success";
    }

    @GetMapping("/test3")
    public User test3(){
        return new User();
    }

    @GetMapping("/test4")
    public ResponseVO test4(){
        return ResultUtil.success(new User());
    }

    @GetMapping("/list")
    public ResponseVO queryUserPageList(User user){
        IPage iPage=userService.queryUserPageList(user);
        String jsonstr=JsonUtils.objectToJson(iPage);
        return ResultUtil.success(iPage);
    }

    @PostMapping("/add")
    public ResponseVO addUser(@RequestBody User user) {
        return userService.addUser(user);
    }

    @GetMapping("/cleanCache")
    public ResponseVO cleanCache(){
        return userService.cheanCache();
    }

    @PostMapping("/dateTest")
    public ResponseVO dateTest(@RequestBody User user){
        return ResultUtil.success(user);
    }

    @PreAuthorize(hasPermi = "test")
    @GetMapping("/testPermission")
    public AjaxResult testPermission(){
        return AjaxResult.success();
    }

//    @GetMapping("/testMail")
//    public AjaxResult testMail(){
//        mailTemplate.sendSimpleMail("1029693356@qq.com","邮件标题","发送邮件测试");
//        return AjaxResult.success();
//    }

//    @GetMapping("/testExportEasyExcel")
//    public void testExportEasyExcel(HttpServletResponse response) {
//        List<User> list = userService.list();
//        EasyExcelUtil.toExcle(response, list,"导出用户列表",User.class);
//    }
//
//    @GetMapping("/testImportEasyExcel")
//    public AjaxResult testImportEasyExcel() throws Exception{
//        File file = new File("E:\\upload\\secret-boot-framework\\upload\\导入用户列表-easyexcel.xlsx");
//        List<User> list = EasyExcelUtil.readExcel(file,User.class);
//        return AjaxResult.success(list);
//    }

//    @GetMapping("/testExportPoi")
//    public AjaxResult testExportPoi(){
//        ExcelUtil<User> excelUtil = new ExcelUtil(User.class);
//        List<User> list = userService.list();
//        List<AbstractDictData> dictList = new ArrayList<>();
//        Dict dict = new Dict();
//        dict.setDictLabel("男");
//        dict.setDictValue("0");
//        dictList.add(dict);
//        DictUtils.setDictCache("user_sex",dictList);
//        return excelUtil.exportExcel(list,"导出用户列表");
//    }

    @GetMapping("/testImportPoi")
    public AjaxResult testImportPoi() throws Exception{
//        StringRedisTemplate rt= SpringContextHolder.getBean(StringRedisTemplate.class);
        List<AbstractDictData> dictList = new ArrayList<>();
        Dict dict = new Dict();
        dict.setDictLabel("男");
        dict.setDictValue("0");
        dictList.add(dict);
        DictUtils.setDictCache("user_sex",dictList);
        ExcelUtil<User> excelUtil = new ExcelUtil(User.class);
        File file = new File("E:\\upload\\secret-boot-framework\\upload\\导入用户列表.xlsx");
        List<User> list = excelUtil.importExcel(FileUtils.openInputStream(file));
        return AjaxResult.success(list);
    }

}
